package com.example.student.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.constants.RdsConstants;
import com.example.common.domain.pojo.Attendance;
import com.example.common.enums.SignInStatus;
import com.example.common.utils.CacheClient;
import com.example.student.mapper.AttendanceMapper;
import com.example.student.service.AttendanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;


@Service
@Slf4j
public class AttendanceServiceImpl extends ServiceImpl<AttendanceMapper, Attendance> implements AttendanceService {

    @Autowired
    private CacheClient cacheClient;
    @Autowired
    private AttendanceMapper attendanceMapper;

    @Override
    public Attendance StudentgetAttendance(String studentId) {
        // 使用逻辑过期策略获取ppt信息
        return cacheClient.queryWithLogicalExpire(
                RdsConstants.ATTENDANCE_INFO,
                studentId,
                Attendance.class,
                this::getByStudentId, // 使用MyBatis-Plus的基础方法
                RdsConstants.CACHE_NULL_TTL,
                TimeUnit.MINUTES,
                false
        );
    }

    Attendance getByStudentId(String studentId) {
        // 构建查询条件
        LambdaQueryWrapper<Attendance> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attendance::getStudentId, studentId);
        // 执行查询
        return attendanceMapper.selectOne(queryWrapper);
    }

    @Override
    public List<Attendance> getByStudentIds(String studentId) {
        LambdaQueryWrapper<Attendance> queryWrapper = new LambdaQueryWrapper<>();
        // 基础条件：学生ID
        queryWrapper.eq(Attendance::getStudentId, studentId);
        // 排序条件：按考勤时间降序
        queryWrapper.orderByDesc(Attendance::getStartTime);

        return attendanceMapper.selectList(queryWrapper);
    }

    @Override
    public boolean IsSignIn(String studentId, String signId, String code) {
        // 1. 构建查询条件
        LambdaQueryWrapper<Attendance> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attendance::getStudentId, studentId)
                .eq(Attendance::getSignId, signId); // 假设Attendance类中有signId字段
        // 2. 执行查询
        Attendance attendance = attendanceMapper.selectOne(queryWrapper);
        // 3. 验证code是否匹配
        if (attendance != null) {
            return code.equals(attendance.getCode());
        }
        // 未找到记录返回false
        return false;
    }

    @Override
    public Attendance signIn(String studentId, String signId) {
        // 1. 构建更新条件
        LambdaQueryWrapper<Attendance> updateWrapper = new LambdaQueryWrapper<>();
        updateWrapper.eq(Attendance::getStudentId, studentId)
                .eq(Attendance::getSignId, signId);

        // 2. 构建要更新的数据（只更新status字段）
        Attendance updateData = new Attendance();
        updateData.setStatus(SignInStatus.SIGNED); // 假设表示已签到
        // 3. 执行更新操作
        boolean success = attendanceMapper.update(updateData, updateWrapper) > 0;
        if (success) {
            // 4. 更新成功后，返回最新的考勤记录
            return getByStudentIdAndClassId(studentId, signId);
        } else {
            // 处理更新失败的情况（如记录不存在）
            log.warn("签到失败，未找到学生ID={}且课程ID={}的考勤记录", studentId, signId);
            return null;
        }
    }

    // 辅助方法：根据学生ID和课程ID查询最新考勤记录
    private Attendance getByStudentIdAndClassId(String studentId, String signId) {
        LambdaQueryWrapper<Attendance> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attendance::getStudentId, studentId)
                .eq(Attendance::getSignId, signId);
        return attendanceMapper.selectOne(queryWrapper);
    }


}
